<template>
  <div class="content">
    <el-row :gutter="10" class="mb8">
      <el-col :span="15">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">
          增加关系人
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="patientList" size="small" style="min-height: 300px">
      <el-table-column label="关系" align="center" prop="relationship">
        <template #default="scope">
          <dict-tag :options="ky_qsgx" :value="scope.row.relationship"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="patientName"/>
      <el-table-column label="联系方式" align="center" prop="phone"/>
      <el-table-column label="性别" align="center" prop="wxysdj">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.wxysdj"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄(岁)" align="center" prop="age"/>
      <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--    <Pagination-->
    <!--        v-show="total > 0"-->
    <!--        :total="total"-->
    <!--        v-model:page="queryParams.pageNum"-->
    <!--        v-model:limit="queryParams.pageSize"-->
    <!--        @pagination="getList"-->
    <!--    />-->

    <el-dialog
        :title="title"
        v-model="open"
        width="800px"
        style=" min-height:500px;"
        :close-on-click-modal="false"
    >
      <el-row>
        <el-form ref="patientRef" :model="addSearchform" label-width="auto" :inline="true">
          <el-form-item label="参与者姓名">
            <el-input v-model="addSearchform.patientName" placeholder="姓名模糊检索"/>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="addSearchform.phone" placeholder="电话号码精确查询"/>
          </el-form-item>
          <el-form-item style="width: 80px">
            <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-radio-group style="width: 100%;" v-model="relationData.otherPatientId">

            <el-table
                ref="refAddTable"
                v-loading="loading"
                :data="patientListAdd"
                size="small"
                height="300px"
                width="600px"
                highlight-current-row
                @current-change="handleSelectionChange"
            >
              <el-table-column label="ID" width="60" align="center" prop="id">
                <template #default="scope">
                  <el-radio :value="scope.row.id">{{ scope.row.id }}</el-radio>
                </template>

              </el-table-column>
              <el-table-column label="姓名" align="center" prop="patientName"/>
              <el-table-column label="联系方式" align="center" prop="phone"/>
              <el-table-column label="性别" align="center" prop="wxysdj">
                <template #default="scope">
                  <dict-tag :options="sys_user_sex" :value="scope.row.wxysdj"/>
                </template>
              </el-table-column>
              <el-table-column label="年龄(岁)" align="center" prop="age"/>
              <el-table-column label="身高(cm)" align="center" prop="height"/>
            </el-table>
          </el-radio-group>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="margin-top: 30px;">
          <el-form-item label="与参与者关系">
            <el-select
                v-model="relationData.relationship"
                placeholder="与参与者关系"
                clearable
            >
              <el-option
                  v-for="dict in ky_qsgx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel" icon="close">取 消</el-button>
          <el-button type="primary" icon="check" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Patient">
import {getRelationship, searchPatient} from "@/api/keyan/patient";
import {addRelationship, delRelationship} from "@/api/keyan/relationship";

import {useRouter} from "vue-router";

const {proxy} = getCurrentInstance();
const {ky_qsgx, ky_jcjb, ky_hrjgl, ky_sf, ky_yjqk, ky_yyxjg, ky_xys, ky_jcjg, sys_user_sex} =
    proxy.useDict("ky_qsgx", "ky_jcjb", "ky_hrjgl", "ky_sf", "ky_yjqk", "ky_yyxjg", "ky_xys", "ky_jcjg", "sys_user_sex");
const patientList = ref([]);
const patientListAdd = ref([]);
const open = ref(false);
const loading = ref(true);
const dialogDoctorListOpen = ref(false);
const showSearch = ref(true);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const refAddTable = ref(null);

const props = defineProps({
  patientId: {
    type: String,
    default: null,
  },
});

const data = reactive({
  addSearchform: {
    id: props.patientId,
  },
  relationData: {
    patientId: props.patientId,
    otherPatientId: "",
    relationship: "",
  },
  rules: {},
});


watch(props, (newProps) => {
  addSearchform.value.id = newProps.patientId;
  relationData.value.patientId = newProps.patientId;
  getList();
});


const router = useRouter();
const {relationData, rules, addSearchform} = toRefs(data);


/** 查询参与者基础信息列表 */
function getList() {
  loading.value = true;
  if (!props.patientId) {
    loading.value = false;
    return;
  }
  getRelationship(props.patientId).then((response) => {
    if (response.code === 200) {
      patientList.value = response.data;
    } else {
      proxy.$modal.msgError(response.msg);
      patientList.value = [];
    }
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
}


/** 搜索按钮操作 */
function handleQuery() {
  console.log('搜索按钮操作:', addSearchform.value);
  searchPatient(addSearchform.value).then((response) => {
    if (response.code == 200) {
      patientListAdd.value = response.data;
    } else {
      proxy.$modal.msgError(response.msg);
      patientListAdd.value = [];
    }
  });
}


/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  relationData.value = {
    patientId: props?.patientId,
    otherPatientId: null,
    relationship: "",
  };
  patientListAdd.value = [];
}

// 多选框选中数据
function handleSelectionChange(selection) {
  // console.log('选择：', selection);
  relationData.value.otherPatientId = selection?.id;
}

/** 提交按钮 */
function submitForm() {
  console.log('提交参数:', relationData);
  if (relationData.value.patientId && relationData.value.otherPatientId && relationData.value.relationship) {
    addRelationship(relationData.value).then((response) => {
      if (response.code === 200) {
        open.value = false;
        getList();
      } else {
        proxy.$modal.msgError("失败：" + response.msg);
      }
    });
  } else {
    proxy.$modal.msgError("请选择参与者或填写关系");
  }

}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.relationId;
  proxy.$modal
      .confirm('是否确认删除"' + row.patientName + '"的数据项？')
      .then(function () {
        return delRelationship(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

onMounted(() => {

  addSearchform.value.id = props.patientId;
  relationData.value.patientId = props.patientId;

  getList();
});

</script>

<style scoped lang="scss">
.el-form-item {
  width: 280px;
}

.content {
  background-color: #FFFFFF;
  padding: 20px;
  border-radius: 5px;
}
</style>
